import BaseFunction as BF
import re
import 搜搜小说网 as ss
import 笔趣阁 as bqg
import 第二课堂 as xbqg


# 网页正则表达式
rebqg = r'https://www.boquge.com\S'
rexbqg = r'http://www.xbiquge.la\S'
ress = r'https://www.soxs.cc\S'


def Check():
    # 从数据库获取数据
    bookcount = BF.selectmssql('select MAX(BID)as total from BooksInfo', None, **BF.sqlparms)[0]['total']
    for i in range(1, bookcount):
        sqlTxt = '''select a.*,b.href
        from UpdateInfo as a,BooksInfo as b
        where a.BID = b.BID and a.BID = %s'''
        row = BF.selectmssql(sqlTxt, i, **BF.sqlparms)
        if len(row) == 0:
            continue
        # 开始执行查询
        Chapters = None
        if re.match(rebqg, row[0]['href'], re.I) is not None:
            Chapters = sorted(list(bqg.ZJIndex(row[0]['href'])), key=lambda c: c.num)
        elif re.match(rexbqg, row[0]['href'], re.I) is not None:
            Chapters = sorted(list(xbqg.GetChapterList(row[0]['href'])), key=lambda c: c.num)
        elif re.match(ress, row[0]['href'], re.I) is not None:
            Chapters = sorted(list(ss.GetChapterList(row[0]['href'])), key=lambda c: c.num)
        if len(Chapters) > row[0]['Num']:
            # State 改为1 需要更新
            BF.myssqlUpdate("update UpdateInfo Set State = 1 where ID = %s", row[0]['ID'], **BF.sqlparms)
            print(row[0]['BID'])
    print('OK')


Check()
